
tempfile bigresults


foreach xxx in 15_30 30_60 60_180 180_300 300_600 600_900 ///
				30_15 60_30 180_60 300_180 600_300 900_600{
	clear
	use "cleandata\winprob_matched_dataset`xxx'"
	
	gen prev_check=strpos(prev_move,"+")>0
	gen prev_capture=strpos(prev_move,"x")>0
	
	keep if winprob!=. & timetaken!=. & timecontrol_iv!=. & size_match==2
	
	//bys matchvar: egen start_clock1=total(start_clock*(timecontrol_iv==0))
	
	//xtile turnbin=turn, n(10)
	
	
	bys matchvar: egen timetaken_1=total(timetaken*timecontrol_iv)
	bys matchvar: egen timetaken_0=total(timetaken*!timecontrol_iv)
	gen firststage=timetaken_1-timetaken_0 if size_match==2
		
	gen marginalvalue_firststage=marginalvalueoftime*firststage
	
	
	tempfile results
	forvalues t=1/5{		
		
		
		ivreghdfe winprob (timetaken=timecontrol_iv) if `t'==elo_percentile, absorb(matchvar) cluster(matchvar)
		
		summ marginalvalue_firststage if `t'==elo_percentile
		local meanmarginalvalue=r(mean)
		summ firststage if marginalvalueoftime!=. & `t'==elo_percentile
		local meanfirststage=r(mean)
		
		summ moverelo if `t'==elo_percentile
		local meanelo=r(mean)
		
		preserve
			parmest, fast level(95)
			gen time="`xxx'"
			gen turnbin=`t'
			gen meanelo=`meanelo'
			
			gen estimate_type="Marginal Benefit"
			//gen marginalvalueoftime=`meanmarginalvalue'/`meanfirststage'
			gen controls="None"
			cap append using `results'
			save `results', replace
		restore
		
		
		ivreghdfe winprob (timetaken=timecontrol_iv) if `t'==elo_percentile, absorb(matchvar#prev_check#prev_capture) cluster(matchvar)
		
		summ marginalvalue_firststage if `t'==elo_percentile
		local meanmarginalvalue=r(mean)
		summ firststage if marginalvalueoftime!=. & `t'==elo_percentile
		local meanfirststage=r(mean)
		
		summ moverelo if `t'==elo_percentile
		local meanelo=r(mean)
		
		preserve
			parmest, fast level(95)
			gen time="`xxx'"
			gen turnbin=`t'
			gen meanelo=`meanelo'
			//gen marginalvalueoftime=`meanmarginalvalue'/`meanfirststage'
			
			gen estimate_type="Marginal Benefit"
			gen controls="Check and Capture"
			cap append using `results'
			save `results', replace
		restore
		
		
		ivreghdfe winprob (timetaken=timecontrol_iv) if `t'==elo_percentile, absorb(matchvar#piecesonboard) cluster(matchvar)
		
		summ marginalvalue_firststage if `t'==elo_percentile
		local meanmarginalvalue=r(mean)
		summ firststage if marginalvalueoftime!=. & `t'==elo_percentile
		local meanfirststage=r(mean)
		
		summ moverelo if `t'==elo_percentile
		local meanelo=r(mean)
		
		preserve
			parmest, fast level(95)
			gen time="`xxx'"
			gen turnbin=`t'
			gen meanelo=`meanelo'
			gen estimate_type="Marginal Benefit"
			//gen marginalvalueoftime=`meanmarginalvalue'/`meanfirststage'
			gen controls="Number of Pieces"
			cap append using `results'
			save `results', replace
		restore
		
		
		
		replace marginalvalue_firststage=0 if timecontrol_iv==0
	
		ivreghdfe marginalvalue_firststage (timetaken= timecontrol_iv) if `t'==elo_percentile, absorb(matchvar) cluster(matchvar)
		
		preserve
			parmest, fast level(95)
			gen time="`xxx'"
			gen start_clockbin=`t'
			gen meanelo=`meanelo'
			gen estimate_type="Marginal Cost"
			//gen marginalvalueoftime=`meanmarginalvalue'/`meanfirststage'
			cap append using `results'
			save `results', replace
		restore
		
	}
	//preserve
	use `results', clear
	summ 
	
	if "`xxx'"=="30_60" | "`xxx'"=="30_15"{
		local mytitle="30 seconds"
	}
	if "`xxx'"=="60_180" | "`xxx'"=="60_30"{
		local mytitle="1 minute"
	}
	if "`xxx'"=="180_300" | "`xxx'"=="180_60"{
		local mytitle="3 minutes"
	}
	if "`xxx'"=="300_180" | "`xxx'"=="300_600"{
		local mytitle="5 minutes"
	}
	if "`xxx'"=="600_300" | "`xxx'"=="600_900"{
		local mytitle="10 minutes"
	}
// 	twoway rspike min95 max95 meanelo, color(forest_green) || ///
// 		scatter estimate meanelo, color(forest_green ) c(l) || ///
// 		line marginalvalueoftime meanelo, lcolor(dkorange) lpattern(dash) ///
// 		name(turns_`xxx', replace) nodraw title("`mytitle'") legend(order(2 3) label(2 "Marginal Benefit") label(3 "Marginal Cost")) ///
// 		xtitle("Elo") yscale(range(0)) ylabel(#3) xlabel(#3) yline(0, lcolor(gs12))

	summ meanelo
	
	replace meanelo=meanelo-(r(max)-r(min))/30 if controls=="None"
	replace meanelo=meanelo+(r(max)-r(min))/30 if controls=="Number of Pieces"
	
	
	twoway rspike min95 max95 meanelo if controls=="None" & estimate_type=="Marginal Benefit", color(forest_green) || ///
		scatter estimate meanelo if controls=="None" & estimate_type=="Marginal Benefit", color(forest_green ) c(l) msym(O) || ///
		rspike min95 max95 meanelo if controls=="Number of Pieces" & estimate_type=="Marginal Benefit", color(maroon) || ///
		scatter estimate meanelo if controls=="Number of Pieces" & estimate_type=="Marginal Benefit", color(maroon) c(l)  msym(S ) || ///
		rspike min95 max95 meanelo if controls=="Check and Capture" & estimate_type=="Marginal Benefit", color(navy) || ///
		scatter estimate meanelo if controls=="Check and Capture" & estimate_type=="Marginal Benefit", color(navy) c(l) msym(T) || ///
		rspike min95 max95 meanelo if estimate_type=="Marginal Cost", color(dkorange) || ///
		scatter estimate meanelo if estimate_type=="Marginal Cost", color(dkorange ) c(l) lpattern(dash) ///
		name(elo_`xxx', replace) nodraw title("`mytitle'") legend(order(2  6 4 8) label(2 "Marginal Benefit") label(6 "Check/Capture Controls") label (4 "Number of Pieces Control") label(8 "Marginal Cost")) ///
		xtitle("Elo") yscale(range(0)) ylabel(#3) xlabel(#3) yline(0, lcolor(gs12))
	
	//restore
	
	cap append using `bigresults'
	save `bigresults', replace
	
}
//
//
// graph combine turns_30_15 turns_60_30 turns_180_60 turns_300_180 turns_600_300 turns_900_600, name(lower_elo, replace)
// graph export "elo_comparison_lower.pdf", as(pdf) replace
//
// graph combine turns_15_30 turns_30_60 turns_60_180 turns_180_300 turns_300_600 turns_600_900, name(higher_elo, replace)
// graph export "elo_comparison_higher.pdf", as(pdf) replace






grc1leg  elo_30_60 elo_60_180 elo_180_300 elo_300_600 elo_600_900 elo_30_15 elo_60_30 elo_180_60 elo_300_180 elo_600_300, row(2) xcommon
graph export "exhibits/elo_comparison_robust.pdf", as(pdf) replace



use `bigresults', clear
